﻿<div class="main">
    <style>
        .checkbox-inline {
            padding-top: 0px
        }
    </style>
    <table border=0 cellpadding=0 cellspacing=0 class="table table-bordered form-inline form-group-sm" id="tb_list" data-source="@Html.GetMemberMenuListJson()">
        <thead>
            <tr>
                <td align="center" style="width:40px"><input type="checkbox" title="选择" class="ui-selectInvert" data-custom-params="isReverse:false"></td>
                <td align=left width="200px"><span>菜单权限/</span><span class="ui-folder-switch">展开</span></td>
                @*<td align="center">类型</td>*@
                <td align="center">页面权限</td>
            </tr>
        </thead>
        <tr class="item list-template-item parent_{parentId}" id='item_{id}' data-parentId="{parentId}" data-isFinal="{isFinal}" data-id="{id}" data-isSys="{isSys}">
            <td align="center"><input type="checkbox" id="id" name="id" value="{id}" class="item_{id} checked-item"></td>
            <td align="center" style="text-align:left;min-width:200px">{space}<span class="node node_{isFinal} node_{id}"></span><span>{name}</span></td>
            <td align=left>
                <div class="sp_item controls checkbox" id="menuitem_{id}" data-isFinal="{isFinal}" data-id="{id}">

                </div>
            </td>
        </tr>
    </table>

    <div class="submit-footerbar">
        <label class="control-label"></label>
        <div class="controls">
            <form id="_form" class=" form-group-sm form ui-validForm" method="post">
                <input type="hidden" name="MenuIds" id="MenuIds" />
                <input type="hidden" name="MenuItemIds" id="MenuItemIds" />
                <input type="hidden" name="Id" id="Id" />
                <input type="hidden" name="MemberGroupId" id="MemberGroupId" value="@Request.QueryString["memberGroupId"]" />
                <button type="button" class="btn btn-sm btn-primary ui-pagePost" data-custom-params="url:'@Url.ActionUrl("Edit")',callBack:'',beforeExecute:'GetSelectedValue',validForm:false">提交</button>
                <button type="button" class="btn btn-sm btn-default ui-closeDialog">关闭</button>
                <span class="help-block"></span>
            </form>
        </div>
</div>
</div>
<script type="text/javascript">
    //绑定管理菜单
    function LoadData() {
        $("#tb_list").TreeBind({hideSubNode:true,saveState:false,callBack:"LoadedInit",dataSourceFrom:"attr",showFooter:false});
    }

    LoadData();

    var $ck = $(".checked-item");
    $ck.click(function () {
        var $this = $(this);
        var id = $this.val();
        if($this.prop("checked"))
        {
            $("#menuitem_" + id).find("input[type='checkbox']").prop("checked",true).removeAttr("disabled");
        }
        else
        {
            $("#menuitem_" + id).find("input[type='checkbox']").prop("checked",false).attr("disabled", "disabled");
        }
    });

    var model =@Html.Raw(Model??"null");
    $("#_form").FormBind({},model);
    var modelMenuids=JsonParse(model).MenuIds;
    var arrModelMenuids=modelMenuids.split(",");

    ///找出所有的menuitem数据
    var menuItemData = Ajax({type:"get",async:false,url:"@Url.ActionUrl("GetCheckBoxList", "MemberMenuItem")"} );
    menuItemData=JsonParse(menuItemData);
    //加载菜单项目
    function LoadMenuItem() {

        //根据guid查找对应的menuitem
        var FindItem= function(adminMenuId) {
            var items = "";
            for (var i = 0; i < menuItemData.length; i++) {
                var jsonData = menuItemData[i];
                var menuId = jsonData.MenuId;
                var disabled=" disabled ";
                var menuItemId = jsonData.Id;
                var menuItemRemark = jsonData.Name;
                if (adminMenuId == menuId) {
                    items += '<label class=\"checkbox-inline\"><input ' + disabled + ' type="checkbox" id="ItemIds" name="ItemIds" value="' + menuItemId + '">' + menuItemRemark +"</label>";
                }
            }
            return items;
        }

        var $sp_item = $(".sp_item");
        $sp_item.each(function () {
            var $this = $(this);
            if ($this.attr("data-isFinal") == "1") {
                var id= $this.attr("data-id");
                var str = FindItem(id);
                $this.html(str);
            }
        });

    }

    if(menuItemData!=null)
    {
        LoadMenuItem();
    }


    var $id = $("#tb_list").find("input[name='id']");
    var $item_ids = $("#tb_list").find("input[name='ItemIds']");

    $id.SetChecked($("#MenuIds").val());

    for(var i=0;i<arrModelMenuids.length;i++)
    {
        $(".node_"+arrModelMenuids[i]).click(); //展开
        var $items=$("#menuitem_"+arrModelMenuids[i]).find("input[type='checkbox']");
        if($items.length>0)
        {
            $items.removeAttr("disabled").SetChecked($("#MenuItemIds").val());
        }
    }


    function GetSelectedValue()
    {
        var menuIds = $id.GetChecked();
        var menuItemIds = $item_ids.GetChecked();
        $("#MenuIds").val(menuIds);
        $("#MenuItemIds").val(menuItemIds);
    }

    $(".ui-selectInvert").on("click",function(){
        var $this=$(this);
        var $itemIds= $("input[name='ItemIds']:visible");
        if($this.prop("checked"))
        {
            $itemIds.prop("checked",true).removeAttr("disabled");
        }
        else
        {
            $itemIds.prop("checked",false).attr("disabled", "disabled");
        }

    });
</script>
